package com.yb.auth.core.handler;

import com.alibaba.fastjson2.JSONObject;
import com.yb.core.bean.Resp;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Copyright (C), 2022-2022, 姚兵
 * Author: 32210
 * Date: 2022/11/13 17:39
 * FileName: AuthFailedHandler
 * Description:
 */
public class AuthFailedHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {

        response.setContentType("application/json;charset=utf-8");

        response.getWriter().write(JSONObject.toJSONString(Resp.ok().data(exception)));

    }
}
